{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-04T13:58:10.187559Z",
     "start_time": "2024-03-04T13:58:10.168925Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"E:/baselines/HS300.csv\")\n",
    "# data = data.drop(columns=['Adj Close'])\n",
    "# data.rename(columns={'Date':'date', 'Open':'open', 'High':'high', 'Low':'low', 'Close':'close', 'Volume':'volume', 'company':'tic'}, inplace=True)\n",
    "data.rename(columns={'dt':'date', 'kdcode':'tic'}, inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T13:58:10.341796Z",
     "start_time": "2024-03-04T13:58:10.188558Z"
    }
   },
   "id": "1334277e44a73176",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "              tic       date  close   open   high    low      turnover  \\\n0       000001.SZ 2018-01-02  13.70  13.35  13.93  13.32  2.856544e+09   \n1       000001.SZ 2018-01-03  13.33  13.73  13.86  13.20  4.006221e+09   \n2       000001.SZ 2018-01-04  13.25  13.32  13.37  13.13  2.454544e+09   \n3       000001.SZ 2018-01-05  13.30  13.21  13.35  13.15  1.603290e+09   \n4       000001.SZ 2018-01-08  12.96  13.25  13.29  12.86  2.806099e+09   \n...           ...        ...    ...    ...    ...    ...           ...   \n203975  603993.SH 2023-12-25   4.93   4.90   4.95   4.87  3.735495e+08   \n203976  603993.SH 2023-12-26   4.92   4.93   4.94   4.89  2.785764e+08   \n203977  603993.SH 2023-12-27   5.08   4.91   5.12   4.91  7.658313e+08   \n203978  603993.SH 2023-12-28   5.16   5.11   5.25   5.10  8.466125e+08   \n203979  603993.SH 2023-12-29   5.20   5.13   5.22   5.13  5.117511e+08   \n\n             volume  \n0       208159255.0  \n1       296249838.0  \n2       185450948.0  \n3       121031272.0  \n4       215862081.0  \n...             ...  \n203975   76115676.0  \n203976   56692336.0  \n203977  151966596.0  \n203978  163415379.0  \n203979   98730274.0  \n\n[203980 rows x 8 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>tic</th>\n      <th>date</th>\n      <th>close</th>\n      <th>open</th>\n      <th>high</th>\n      <th>low</th>\n      <th>turnover</th>\n      <th>volume</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>000001.SZ</td>\n      <td>2018-01-02</td>\n      <td>13.70</td>\n      <td>13.35</td>\n      <td>13.93</td>\n      <td>13.32</td>\n      <td>2.856544e+09</td>\n      <td>208159255.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>000001.SZ</td>\n      <td>2018-01-03</td>\n      <td>13.33</td>\n      <td>13.73</td>\n      <td>13.86</td>\n      <td>13.20</td>\n      <td>4.006221e+09</td>\n      <td>296249838.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>000001.SZ</td>\n      <td>2018-01-04</td>\n      <td>13.25</td>\n      <td>13.32</td>\n      <td>13.37</td>\n      <td>13.13</td>\n      <td>2.454544e+09</td>\n      <td>185450948.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>000001.SZ</td>\n      <td>2018-01-05</td>\n      <td>13.30</td>\n      <td>13.21</td>\n      <td>13.35</td>\n      <td>13.15</td>\n      <td>1.603290e+09</td>\n      <td>121031272.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>000001.SZ</td>\n      <td>2018-01-08</td>\n      <td>12.96</td>\n      <td>13.25</td>\n      <td>13.29</td>\n      <td>12.86</td>\n      <td>2.806099e+09</td>\n      <td>215862081.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>203975</th>\n      <td>603993.SH</td>\n      <td>2023-12-25</td>\n      <td>4.93</td>\n      <td>4.90</td>\n      <td>4.95</td>\n      <td>4.87</td>\n      <td>3.735495e+08</td>\n      <td>76115676.0</td>\n    </tr>\n    <tr>\n      <th>203976</th>\n      <td>603993.SH</td>\n      <td>2023-12-26</td>\n      <td>4.92</td>\n      <td>4.93</td>\n      <td>4.94</td>\n      <td>4.89</td>\n      <td>2.785764e+08</td>\n      <td>56692336.0</td>\n    </tr>\n    <tr>\n      <th>203977</th>\n      <td>603993.SH</td>\n      <td>2023-12-27</td>\n      <td>5.08</td>\n      <td>4.91</td>\n      <td>5.12</td>\n      <td>4.91</td>\n      <td>7.658313e+08</td>\n      <td>151966596.0</td>\n    </tr>\n    <tr>\n      <th>203978</th>\n      <td>603993.SH</td>\n      <td>2023-12-28</td>\n      <td>5.16</td>\n      <td>5.11</td>\n      <td>5.25</td>\n      <td>5.10</td>\n      <td>8.466125e+08</td>\n      <td>163415379.0</td>\n    </tr>\n    <tr>\n      <th>203979</th>\n      <td>603993.SH</td>\n      <td>2023-12-29</td>\n      <td>5.20</td>\n      <td>5.13</td>\n      <td>5.22</td>\n      <td>5.13</td>\n      <td>5.117511e+08</td>\n      <td>98730274.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>203980 rows × 8 columns</p>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['date'] = pd.to_datetime(data['date'])\n",
    "grouped_df = data.groupby('tic')\n",
    "l = data['date'].nunique()\n",
    "def filter_groups(group):\n",
    "    return len(group) == l\n",
    "df = grouped_df.filter(filter_groups)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T13:58:10.468158Z",
     "start_time": "2024-03-04T13:58:10.343859Z"
    }
   },
   "id": "39d85b13b2cc05c5",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "140"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['tic'].nunique()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T13:58:10.499061Z",
     "start_time": "2024-03-04T13:58:10.470158Z"
    }
   },
   "id": "6b15281b5fb1aca9",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "1091   2022-07-04\n1092   2022-07-05\n1093   2022-07-06\n1094   2022-07-07\n1095   2022-07-08\n          ...    \n1452   2023-12-25\n1453   2023-12-26\n1454   2023-12-27\n1455   2023-12-28\n1456   2023-12-29\nName: date, Length: 366, dtype: datetime64[ns]"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_unique = pd.DataFrame({'date': df['date'].unique()})\n",
    "df_unique['2022-07-01' < df_unique['date']]['date']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T13:58:10.514064Z",
     "start_time": "2024-03-04T13:58:10.500061Z"
    }
   },
   "id": "80edd7c144e722cc",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 140/140 [00:02<00:00, 49.88it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler()\n",
    "cols_to_normalize = ['close', 'open', 'high', 'low', 'volume', 'turnover']\n",
    "\n",
    "for stock in tqdm(df['tic'].unique()):\n",
    "    tmp = df[df['tic'] == stock].reset_index(drop=True)\n",
    "    # tmp[cols_to_normalize] = scaler.fit_transform(tmp[cols_to_normalize])\n",
    "    tmp.to_csv(f\"hs300/{stock}.csv\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T13:58:13.353316Z",
     "start_time": "2024-03-04T13:58:10.516065Z"
    }
   },
   "id": "e95a11ce9b33203c",
   "execution_count": 12
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
